<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\middleware\admin\Auth;
use app\middleware\api\CheckLogin;
use app\model\Product;
use think\facade\Db;
use think\Request;

class Category extends BaseController
{
    protected $middleware = [
        CheckLogin::class=>['only'=>['getProducts']]
    ];
    public function index()
    {
       return $this->success( \app\model\Category::select());
    }

    /**
     * 获取分类下的产品
     * @param $id
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProducts($limit=10)
    {

        (new \app\validate\api\Category())->goCheck();
        $id=request()->param('id');
       return $this->success( Product::with('collect')->where('category_id',$id)->paginate($limit));
    }

}
